package com.umiwe.permission.controller;

import com.umiwe.entity.UmiweUser;
import com.umiwe.entity.UmiweUserVO;
import com.umiwe.permission.service.DingDingService;
import com.umiwe.permission.service.UserService;
import com.umiwe.utils.JwtUtils;
import com.umiwe.utils.Result;
import com.umiwe.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "用户模块",tags = "UserController")
@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DingDingService dingDingService;

    /**
     * liximing
     */
    @PostMapping("/add")
    @ApiOperation(value = "获取部门用户详情 执行添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门id", required = true, paramType = "form", dataType = "Integer"),
    })
    public Result addUser(@RequestParam(name = "departmentId") Integer departmentId){
        if(departmentId == null)
            return Result.error(ResultCode.DEPARTMENT_ID);
        if(userService.addUser(departmentId,0L,100L))
            return Result.success(true);
        return Result.error(ResultCode.DEPARTMENT_FAIL);
    }

    /**
     * liximing
     */
    @ApiOperation(value="钉钉扫码获取用户信息")
    @PostMapping("/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "授权code", required = true, paramType = "form", dataType = "String"),
    })
    public Result getUserId(@RequestParam(name = "code") String code){
        if(StringUtils.isBlank(code))
            return Result.error(ResultCode.USER_CODE);
        return userService.getUserId(code);
    }

    /**
     * liximing
     */
    @ApiOperation(value="是否token是否过期")
    @PostMapping("/verification/token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "form", dataType = "String"),
    })
    public Result verificationToken(String token){
        if(StringUtils.isBlank(token))
            return Result.error(ResultCode.USER_TOKEN_NULL);
        return Result.success(JwtUtils.verity(token));
    }

    /**
     * liximing
     */
    @PostMapping("/login")
    @ApiOperation(value="识别登录密码是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id 就是dingid", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "form", dataType = "String"),
    })
    public Result login(@RequestParam(name = "userId") String userId,@RequestParam(name = "password") String password){
        if(StringUtils.isBlank(userId))
            return Result.error(ResultCode.USER_ID_NULL);
        if(StringUtils.isBlank(password))
            return Result.error(ResultCode.USER_PASSWROD_NULL);
        return userService.login(userId,password);
    }

    /**
     * @param umiweUserVO
     * @return
     */
    @PostMapping("/update/password")
    @ApiOperation(value="修改密码")
    public Result updatePassWord(@RequestBody UmiweUserVO umiweUserVO){
        if(userService.updatePassWord(umiweUserVO))
        return Result.success(ResultCode.SUCCESS);
        return Result.error(ResultCode.USET_UPDATA_FAIL);
    }

    /**
     * liximing
     */
    //获取token
    @GetMapping("/get/token")
    @ApiOperation(value="钉钉token授权")
    public Result getDingToken(){
        return Result.success(dingDingService.getDingDingToken());
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public UmiweUser getUserById(@PathVariable("id") Integer id){
        return userService.getUserById(id);
    }


}
